﻿//------------------------------------------------------------------------------
// 请不要手动修改此文件中的代码
//------------------------------------------------------------------------------

using Corp.Proj.Model;
using Corp.Proj.IDAL;
using System.Configuration;
using System.Reflection;
using Corp.Proj.DAL;

namespace Corp.Proj.DALFactory
{
	public partial class DbSession : IDbSession
	{
		private readonly static string assemblyName = ConfigurationManager.AppSettings["AssemblyName"].ToString();
        private readonly static string nameSpace = ConfigurationManager.AppSettings["NameSpace"].ToString();

		/// <summary>
        /// 根据程序集名与类的完全限定名，获得实例类对象
        /// </summary>
        /// <param name="assemblyName"></param>
        /// <param name="classFullName"></param>
        /// <returns></returns>
        public static object GetInstances(string assemblyName, string classFullName)
        {
            #region 程序集不能放到缓存中，原因是根据一个程序集得到的对象都是一样，在进行SaveChanges的时候，会造成问题
            //object obj = CacheHelper.Get(classFullName);
            //if (obj != null)
            //{
            //    obj = Assembly.Load(assemblyName).CreateInstance(classFullName);//根据反射获取实例类
            //    CacheHelper.Set(classFullName, obj);//将生成的实例类，存在于缓存中
            //}
            //return obj; 
            #endregion
            object obj = Assembly.Load(assemblyName).CreateInstance(classFullName);//根据反射获取实例类
            return obj;
        }

		public int SaveChanges()
        {
            //DbContext db = EFDbContextFactory.GetCurrentDbContext().sa;
            return EFDbContextFactory.GetCurrentDbContext().SaveChanges();
        }

	    public ITDemoDal TDemoDal
        {
            get
            {
                return GetInstances(assemblyName, nameSpace + ".TDemoDal") as ITDemoDal;
            }
        }
	    public ITDemo2Dal TDemo2Dal
        {
            get
            {
                return GetInstances(assemblyName, nameSpace + ".TDemo2Dal") as ITDemo2Dal;
            }
        }
	}
}//end namespace